package pl.engine.ui.gui
{
	import pl.engine.camera.Camera;
	import pl.engine.ui.UI;
	import pl.engine.view.View;
	import pl.error.InternalError;
	import pl.textures.TextureID;
	import pl.textures.Textures;

	import starling.core.Starling;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;

	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import flash.ui.MouseCursorData;

	/**
	 * ...
	 * @author Bakudo
	 */
	public class Cursor implements ICursor
	{
		public static const SWORD:String = 'sword';
		public static const CURSOR:String = 'cursor';
		public static const CURSOR_DRAG:String = 'cursorDrag';
		private var position:Point = new Point();
		private var screenPosition:Point = new Point();
		private static var secureFunction:Function;
		private static var _isReleased:Boolean;
		private static var _isClicked:Boolean;

		public function Cursor()
		{
			if (secureFunction == null) throw new Error( InternalError.CREATION_ERROR );
			var mcd:MouseCursorData = new MouseCursorData();

			var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>( 1, true );
			bitmapDatas[0] = Textures.getSingleTexture( TextureID.CURSOR ).bitmapData;
			mcd.data = bitmapDatas;
			Mouse.registerCursor( SWORD, mcd );

			bitmapDatas = new Vector.<BitmapData>( 1, true );
			bitmapDatas[0] = Textures.getSingleTexture( TextureID.CURSOR_ARROW ).bitmapData;
			mcd = new MouseCursorData();
			mcd.data = bitmapDatas;
			Mouse.registerCursor( CURSOR, mcd );

			bitmapDatas = new Vector.<BitmapData>( 1, true );
			bitmapDatas[0] = Textures.getSingleTexture( TextureID.CURSOR_DRAG ).bitmapData;
			mcd = new MouseCursorData();
			mcd.data = bitmapDatas;

			_isReleased = true;
			Mouse.registerCursor( CURSOR_DRAG, mcd );

			Starling.current.stage.addEventListener( TouchEvent.TOUCH, onMM );
		}

		public static function factory():ICursor
		{
			secureFunction = function():void
			{
			};
			var c:Cursor = new Cursor();
			return c;
		}

		public function setSwordCursor():void
		{
			Mouse.cursor = SWORD;
		}

		public function setDragCursor():void
		{
			Mouse.cursor = Cursor.CURSOR_DRAG;
		}

		public function setNormalCursor():void
		{
			Mouse.cursor = CURSOR;
		}

		public function getIsoPosition():Point
		{
			return position;
		}

		public function getScreenPosition():Point
		{
			return screenPosition;
		}

		public function get isReleased():Boolean
		{
			return _isReleased;
		}

		public function getRectPosition():Point
		{
			return Camera.convertIsoToRect(position.x, position.y);
		}

		private function onMM( e:TouchEvent ):void
		{
			var touch:Touch = e.getTouch( View.getView() );
			if (touch != null)
			{
				position = touch.getLocation( View.getView() );
				screenPosition = e.getTouch( View.getView().stage ).getLocation( View.getView().stage );
				switch(touch.phase)
				{
					case TouchPhase.ENDED:
						_isReleased = true;
						break;
					case TouchPhase.BEGAN:
						_isReleased = false;
						break;
				}
				UI.invokeChangeHandler( this );
			}
		}
	}
}